﻿Imports DAO
Imports BUS
Imports DTO
Imports DevComponents.DotNetBar
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Diagnostics

Public Class frmQuanLyKhachHang

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim kh As New KhachHangDTO()
        Try
            If txtTenKH.Text <> "" Then
                kh.HoTenKhachHang = txtTenKH.Text
            Else
                MessageBoxEx.Show("Tên khách hàng không được để trống", "Thông báo")
                Return
            End If
            If txtDienThoai.Text <> "" Then
                kh.DienThoai = Integer.Parse(txtDienThoai.Text)
            Else
                MessageBoxEx.Show("Điện thoại không được để trống", "Thông báo")
                Return
            End If
            If txtDiaChi.Text <> "" Then
                kh.DiaChi = txtDiaChi.Text
            Else
                MessageBoxEx.Show("Ðịa chỉ không được để trống", "Thông báo")
                Return
            End If
            If txtEmail.Text <> "" Then
                kh.Email = txtEmail.Text
            Else
                MessageBoxEx.Show("Email không được để trống", "Thông báo")
                Return
            End If


        Catch e1 As FormatException
            MessageBoxEx.Show("Nhập vào số", "Thông báo")
            Return
        End Try

        If KhachHangBUS.ThemKhachHang(kh) = False Then
            MessageBoxEx.Show("Dữ liệu đã có trong CSDL", "Thông báo")
            Return
        End If
        HienThiDanhSach()
    End Sub

    Public Sub HienThiDanhSach()
        dgvDanhSachKhachHang.DataSource = KhachHangDAO.GetKhachHangAll()
    End Sub

    Private Sub frmQuanLyKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HienThiDanhSach()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim kh As New KhachHangDTO()

        kh.MaKhachHang = Integer.Parse(txtMaKH.Text)
        If KhachHangBUS.XoaKhachHang(kh) = False Then
            MessageBoxEx.Show("Xóa thất bại", "Thông báo")
        End If
        HienThiDanhSach()
    End Sub

    Private Sub dgvDanhSachKhachHang_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachKhachHang.SelectionChanged
        If dgvDanhSachKhachHang.SelectedRows.Count > 0 Then
            txtMaKH.Text = dgvDanhSachKhachHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenKH.Text = dgvDanhSachKhachHang.SelectedRows(0).Cells(1).Value.ToString()
            txtDienThoai.Text = dgvDanhSachKhachHang.SelectedRows(0).Cells(2).Value.ToString()
            txtDiaChi.Text = dgvDanhSachKhachHang.SelectedRows(0).Cells(3).Value.ToString()
            txtEmail.Text = dgvDanhSachKhachHang.SelectedRows(0).Cells(4).Value.ToString()
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim kh As New KhachHangDTO()
        kh.MaKhachHang = Integer.Parse(txtMaKH.Text)
        kh.HoTenKhachHang = txtTenKH.Text
        kh.DienThoai = Integer.Parse(txtDienThoai.Text)
        kh.DiaChi = txtDiaChi.Text
        kh.Email = txtEmail.Text
        If KhachHangBUS.SuaKhachHang(kh) = False Then
            MessageBoxEx.Show("Cập nhật thất bại", "Thông báo")
        End If
        HienThiDanhSach()
    End Sub

    Private Sub txtTimKiem_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTimKiem.TextChanged
        If radtenkh.Checked = True Then
            timkiemtenkh()
        End If
        If raddiachi.Checked = True Then
            timkiemdiachi()
        End If
        If raddienthoai.Checked = True Then
            timkiemdienthoai()
        End If
        If rademail.Checked = True Then
            timkiememail()
        End If
    End Sub


    Private Sub radtenkh_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radtenkh.CheckedChanged
        timkiemtenkh()
    End Sub

    Private Sub raddiachi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles raddiachi.CheckedChanged
        timkiemdiachi()
    End Sub

    Private Sub raddienthoai_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles raddienthoai.CheckedChanged
        txtTimKiem.Text = "0"
        timkiemdienthoai()
    End Sub

    Private Sub rademail_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rademail.CheckedChanged
        timkiememail()
    End Sub

    Private Sub btnXoaTrang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTrang.Click
        txtMaKH.Clear()
        txtDienThoai.Clear()
        txtTenKH.Clear()
        txtEmail.Clear()
        txtDiaChi.Clear()
    End Sub

    Private Sub btnDanhSachKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDanhSachKhachHang.Click
        HienThiDanhSach()
    End Sub

    Public Sub timkiemtenkh()

        Dim kh As New KhachHangDTO()
        kh.HoTenKhachHang = txttimkiem.Text
        dgvDanhSachKhachHang.DataSource = KhachHangDAO.SelectKhachHangLikeTen(kh)
    End Sub
    Public Sub timkiemdiachi()

        Dim kh As New KhachHangDTO()
        kh.DiaChi = txttimkiem.Text
        dgvDanhSachKhachHang.DataSource = KhachHangDAO.SelectKhachHangLikeDiaChi(kh)
    End Sub
    Public Sub timkiememail()

        Dim kh As New KhachHangDTO()
        kh.Email = txttimkiem.Text
        dgvDanhSachKhachHang.DataSource = KhachHangDAO.SelectKhachHangLikeEmail(kh)
    End Sub
    Public Sub timkiemdienthoai()

        Dim kh As New KhachHangDTO()
        Try
            kh.DienThoai = Int64.Parse(txttimkiem.Text)
        Catch e1 As FormatException
            MessageBoxEx.Show("Ðiện thoại phải điền vào số", "Thông báo")
            Return
        End Try
        dgvDanhSachKhachHang.DataSource = KhachHangDAO.SelectKhachHangLikeDienThoai(kh)
    End Sub

    Private Sub btnNhapVao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapVao.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim [of] As New OpenFileDialog()
        [of].ShowDialog()
        Dim strPathName As String = [of].FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim dt As DataTable = ExcelRead.getSheet(strPathName, "sheet1")
        For i As Integer = 0 To dt.Rows.Count - 1
            Dim kh As New KhachHangDTO()
            kh.HoTenKhachHang = dt.Rows(i).ItemArray(1).ToString()
            kh.DienThoai = Integer.Parse(dt.Rows(i).ItemArray(2).ToString())
            kh.DiaChi = dt.Rows(i).ItemArray(3).ToString()
            kh.Email = dt.Rows(i).ItemArray(4).ToString()
            KhachHangDAO.Insert(kh)
        Next i
        MessageBoxEx.Show("Nhập danh sách khách hàng từ Excel thành công", "Thông báo")
        HienThiDanhSach()
    End Sub

    Private Sub btnXuatFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuatFile.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim sf As New SaveFileDialog()
        sf.ShowDialog()
        Dim strFileName As String = sf.FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim myExcel As New ExcelWrite()
        Dim list As List(Of String) = myExcel.DGVToExcel(dgvDanhSachKhachHang)
        Dim filePath As String = Directory.GetCurrentDirectory() & strFileName
        myExcel.WriteDateToExcel(strFileName, list, "A1", "E1", "A2")
        MessageBoxEx.Show("Xuất danh sách khách hàng ra file Excel thành công", "Thông báo")
        'Process.Start(strFileName)
    End Sub
End Class